package pers.ht.mbpo.service;

import net.sf.json.JSONArray;
import pers.ht.mbpo.bean.BusCar;
import pers.ht.mbpo.bean.BusEnvironment;
import pers.ht.mbpo.dao.BusCarDao;
import pers.ht.mbpo.dao.BusEnvironmentDao;
import pers.ht.mbpo.util.ExcelUtil;
import pers.ht.mbpo.util.UploadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/BusEServlet")
public class BusEServlet extends HttpServlet {
    private BusEnvironmentDao bEDao = new BusEnvironmentDao();
    private BusCarDao bCDao = new BusCarDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");
        if ("upload".equals(action)) {
            PrintWriter out = response.getWriter();
            String filePath = UploadUtil.saveFile(request);
//            List<String[]> es = ExcelUtil.impExcel(0, filePath);
//            if (es.size() > 0) {
//                bEDao.delete();
//                for (String[] strings : es) {
//                    bEDao.insert(new BusEnvironment(strings[0], strings[1], strings[2], strings[3], strings[4], strings[5], strings[6], strings[7], strings[8], strings[9], strings[10], strings[11]));
//                }
//            }
            List<String[]> cs = ExcelUtil.impExcel(1, filePath);
            if (cs.size() > 0) {
                String carid="";
                for (String[] strings : cs) {
                    if(!strings[1].equals(carid)){
                        carid=strings[1];
                        bCDao.delete(carid);
                    }

                    bCDao.insert(new BusCar(strings[0], strings[1], strings[2], strings[3], strings[4], strings[5], strings[6], strings[7], strings[8], strings[9],strings[10],strings[11]));
                }
            }
            out.println(UploadUtil.getJson());
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, DELETE,PUT,GET,OPTIONS");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String time = request.getParameter("time");
        String updown = request.getParameter("updown");
        String busLineName = request.getParameter("busLineName");
        List<BusEnvironment> environmentList = bEDao.query(time,busLineName, updown);
        String scoreJson = JSONArray.fromObject(environmentList).toString();
        out.print(scoreJson);
    }
}
